﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Infragistics.WebUI.UltraWebGrid;
using Infragistics.WebUI.WebCombo;

public partial class page_DepotIN_Edit : PageBase
{
    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        this.RegisterJS();
                
        if (!IsPostBack)
        {
            this.InitComponents();                      //初始化组件
            this.LoadShop();                            //加载店铺信息
            this.ReadAction();                          //保存传过来的值  
            this.SetPageNoCache();
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        isAtFramePageLogin = true;                              //是框架页

        //RequestCompetences = Competences.BuildCompetencesList(Competences.DepotIN_Manager);//页面权限

        base.OnLoad(e);
    }

    #endregion

    protected void UWGDetail_UpdateRow(object sender, RowEventArgs e)
    {
        if (e.Row.Cells.FromKey("GoodNo").Value != null)
        {
            long DetailID = 0;
            long GoodID = 0;
            DAL.Tables.tbl_Shop_DepotINDetail details = new DAL.Tables.tbl_Shop_DepotINDetail();

            try
            {
                DetailID = Shove._Convert.StrToLong(e.Row.Cells.FromKey("ID").ToString(), 0);         //采购入库单明细表ID
                GoodID = Shove._Convert.StrToLong(e.Row.Cells.FromKey("GoodID").ToString(), 0);       

                if (this.hAction.Value == "Received")
                {
                    //实际到货数量(不修改则表示没有运输损耗)
                    details.ActualAmount.Value = UWGDetail.DisplayLayout.ActiveRow.Cells.FromKey("ActualAmount").ToString();
 
                    details.Update("ID=" + DetailID +" and GoodID="+GoodID);
                }
            }
            catch { }
        }
    }

    protected void btnSaveMaster_Click(object sender, EventArgs e)
    {
        if (ddlInvoiceNO.SelectedValue == "0")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请选择关联的订单号！");
            return;
        }

        trDetail1.Visible = true;
        trDetail2.Visible = true;

        long CreateOperID = _Customer.ID;               //制单人
        string Remark = tbRemark.Text;

        if (hAction.Value == "Edit")                    //更新
        {       
            long ID = Shove._Convert.StrToLong(hID.Value, 0);

            DAL.Tables.tbl_Shop_DepotINMaster dtMaster = new DAL.Tables.tbl_Shop_DepotINMaster();

            dtMaster.Remark.Value = Remark;

            long i = dtMaster.Update("[ID]=" + ID);

            if (i >= 0)
            {
                Response.Write("<script language='javascript'>alert('数据保存成功！');self.location='DepotIN_Manager.aspx';</script>");
                Response.Flush();
            }
        }
        else                                            //添加
        {
            long tempID =0;
            DAL.Procedures.P_Shop_AddDepotIN_Master(Shove._Convert.StrToLong(ddlInvoiceNO.SelectedValue, 0), _Customer.ID,tbRemark.Text, ref tempID);
            hID.Value = tempID.ToString();
            hAction.Value = "Edit";                     //修改状态为编缉
        }

        this.BindData();
        this.BindDataDetail();
    }

    protected void btnCancelMaster_Click(object sender, EventArgs e)
    {
        Response.Redirect("DepotIN_Manager.aspx");
    }

    protected void btnMimeograph_Click(object sender, EventArgs e)
    {       
        string ID = hID.Value;

        Response.Write("<script language='javascript'>window.open('Depot_IN_Print.aspx?ID=" + ID + "')</script>");
    }

    protected void btnExporterExcel_Click(object sender, EventArgs e)
    {
        Utility.DataTableToExcel.DataTableToExcelAndDownload(dt, "Depot_IN_Edit.xls");
    }

    /// <summary>
    /// 注册JS
    /// </summary>
    private void RegisterJS()
    {
      //  AjaxPro.Utility.RegisterTypeForAjax(typeof(Ajax.Good));  //注册Ajax方法

        Infragistics.WebUI.Shared.Util.ClientScript.ResolveAndRegisterClientScriptInclude(
             Shove._Web.Utility.GetUrl() + "/js/Scripts/ig_webgrid_an.js",
             "",
             Shove._Web.Utility.GetUrl() + "/js/Scripts/ig_webgrid_an.js",
             "ig_webgrid_an.js", this.UWGDetail, "ig_webgrid_an");          //注册 Infragistics js和控件  
    }

    /// <summary>
    /// 初始化组件
    /// </summary>
    private void InitComponents()
    {
        trOrderInfo.Visible = false;
        trDetail1.Visible = false;
        trDetail2.Visible = false;

        Utility.InitUltraWebGrid.InitDisplayLayOut_Oper(this.UWGDetail);
    }

    /// <summary>
    /// 保存传过来的参数
    /// </summary>
    private void ReadAction()
    {
        String sAction = Shove._Web.Utility.GetRequest("Action");
        this.hAction.Value = sAction;

        String sID = Shove._Web.Utility.GetRequest("ID");
        this.hID.Value = sID;

        if (sAction == "Received")
        {
            this.UWGDetail.Columns[7].Hidden = false;
        }

        if (this.hAction.Value == "Edit" || this.hAction.Value == "Received")
        {
            trOrderInfo.Visible = true;
            trDetail1.Visible = true;
            trDetail2.Visible = true;

            BindData();
            BindDataDetail();
        }
        else if (this.hAction.Value == "Add")
        {
            DataSet ds = new DataSet();
            DAL.Procedures.P_Shop_GetInInvoiceNO(ref ds,_Site.ID);
            if (ds.Tables.Count > 0)
            {
                ddlInvoiceNO.DataSource = ds.Tables[0];
                ddlInvoiceNO.DataTextField = "OrderNO";
                ddlInvoiceNO.DataValueField = "ID";
                ddlInvoiceNO.DataBind();
            }
            ddlInvoiceNO.Items.Insert(0, new ListItem("--请选择单据--", "0"));
        }
    }

    /// <summary>
    /// 加载该订单下的所有商品信息
    /// </summary>
    private void BindDataDetail()
    {
        string sID = hID.Value; //订单ID

        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Clear();
        }

        dt = new DAL.Tables.tbl_Shop_DepotINDetail().Open("", "INID=" + sID, ""); 

        UWGDetail.DataSource = dt;
        UWGDetail.DataBind();
    }

    /// <summary>
    /// 初始化订单表头信息
    /// </summary>
    private void BindData()
    {
        string sID = hID.Value; //订单ID
        DataTable dt = new DAL.Views.V_Shop_DepotINMaster().Open("","ID="+sID,"");

        if (dt != null&& dt.Rows.Count > 0)
        {
            ddlNO.Items.Clear();
            ddlNO.Items.Add(new ListItem(dt.Rows[0]["InNo"].ToString(), sID));

            string InvoiceNO = "";//P_Shop_GetAllocateInvoiceNOByID
            DAL.Procedures.P_Shop_GetAllocateInvoiceNOByID(Shove._Convert.StrToLong(sID, 0), ref InvoiceNO);
            ddlInvoiceNO.Items.Clear();
            ddlInvoiceNO.Items.Add(new ListItem(InvoiceNO, dt.Rows[0]["InvoiceID"].ToString()));

            lbMoney.Text = dt.Rows[0]["ActualMoney"].ToString();
            tbRemark.Text = dt.Rows[0]["Remark"].ToString();
            ddlTargetShop.SelectedValue = dt.Rows[0]["TargetShopID"].ToString();
            ddlSourceShop.SelectedValue = dt.Rows[0]["SourceShopID"].ToString();
            dt = null;
        }
    }

    /// <summary>
    /// 加载店铺信息
    /// </summary>
    private void LoadShop()
    {
        DataTable dt = new DAL.Tables.tbl_Shop().Open("", "Status=1", "");

        ddlSourceShop.DataTextField = "ShopName";
        ddlSourceShop.DataValueField = "ID";

        ddlTargetShop.DataTextField = "ShopName";
        ddlTargetShop.DataValueField = "ID";

        ddlSourceShop.DataSource = dt;
        ddlTargetShop.DataSource = dt;

        ddlSourceShop.DataBind();
        ddlTargetShop.DataBind();

        ddlSourceShop.Items.Insert(0,new ListItem("--源机构--","0"));
        ddlTargetShop.Items.Insert(0, new ListItem("--目标机构--", "0"));
    }

    /// </summary>
    /// 设置页面不被缓存
    /// </summary>
    public void SetPageNoCache()
    {
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.Expires = 0;
        HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        HttpContext.Current.Response.AddHeader("pragma", "no-cache");
        HttpContext.Current.Response.AddHeader("cache-control", "private");
        HttpContext.Current.Response.CacheControl = "no-cache";
    }

    /// <summary>
    /// 选择加载关联订单信息
    /// </summary>
    protected void ddlInvoiceNO_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sID = ddlInvoiceNO.SelectedValue;

        if (sID != "0")
        {
            DataTable dt = new DAL.Tables.tbl_Shop_DepotOutMaster().Open("", "ID=" + sID, "");

            ddlSourceShop.SelectedValue = dt.Rows[0]["SourceShopID"].ToString();
            ddlTargetShop.SelectedValue = dt.Rows[0]["TargetShopID"].ToString();
        }
    }
}


